package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.ComplianceDateRefer;
import com.originskyseed.platform.user.model.ComplianceDateReferAnnex;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IComplianceDateReferAnnexService;
import com.originskyseed.platform.user.service.IComplianceDateReferService;
import com.originskyseed.platform.user.vo.request.ComplianceDateReferRequestVO;

/**
 * 合规资料提交信息Controller类（OSI_COMPLIANCE_DATA_REFER）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/complianceDateRefer")
@BaseLocaleResource(localeFileName = "i18n_user")
public class ComplianceDateReferController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ComplianceDateReferController.class);

	@DubboReference
	private IComplianceDateReferService complianceDateReferService;

	@DubboReference
	private IComplianceDateReferAnnexService complianceDateReferAnnexService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complianceDateReferList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<ComplianceDateRefer>> complianceDateReferList(HttpServletRequest request) {
		BaseResponseBody<BasePage<ComplianceDateRefer>> result = null;
		try {
			// 请求对象
			ComplianceDateReferRequestVO requestVO = this.getParameterBean(ComplianceDateReferRequestVO.class);

			// 分页查询
			BasePage<ComplianceDateRefer> page = new BasePage<ComplianceDateRefer>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.complianceDateReferService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<ComplianceDateRefer>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<ComplianceDateRefer>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息_合规资料提交
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complianceDateReferSave.json")
	@RequiredParams(names = { "AnnexList" })
	public BaseResponseBody complianceDateReferSave(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			ComplianceDateRefer complianceDateRefer = this.getParameterBean(ComplianceDateRefer.class);
			List<ComplianceDateReferAnnex> AnnexList = this.getParameterList("AnnexList", ComplianceDateReferAnnex.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			if (complianceDateRefer.getStartTime() != null && complianceDateRefer.getEndTime() != null) {
				String primaryKey = PrimaryKeyGeneratorUtil.getInstance().getRandomKey();
				complianceDateRefer.setId(primaryKey);// 主键
				complianceDateRefer.setCreator(userInfo.getName());
				complianceDateRefer.setCreatorId(userInfo.getId());
				complianceDateRefer.setStatus(0);// 待审核
				this.complianceDateReferService.insertSelective(complianceDateRefer);
				if (AnnexList.size() > 0) {
					for (int i = 0; i < AnnexList.size(); i++) {
						ComplianceDateReferAnnex referAnnex = AnnexList.get(i);
						referAnnex.setComReferId(primaryKey);
						this.complianceDateReferAnnexService.insertSelective(referAnnex);
					}
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complianceDateReferDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> complianceDateReferDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.complianceDateReferService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
